package com.tronsis.library.widget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tronsis.library.R;


/**
 * 自定义的加载对话框
 * @author allen@tronsis.com
 */
public class ConfirmDialog implements OnClickListener {

    private Context mContext;
    private Dialog mDialog;
    private TextView mTitleTv;
    private Button mLeftBtn;
    private Button mRightBtn;
    private OnBottomButtonClickListener listener;
    private ImageView mCentreIconIv;


    public interface OnBottomButtonClickListener {
        public void onLeftButtonClick(View view);

        public void onRightButtonClick(View view);
    }

    public ConfirmDialog(Context context, OnBottomButtonClickListener listener) {
        this.mContext = context;
        this.listener = listener;
        this.mDialog = new AlertDialog.Builder(context, R.style.ConfirmDialog).create();
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setCancelable(true);
    }

    /**
     * 显示对话框
     * @param titleText
     * @param buttonLeftText
     * @param buttonRightText
     */
    public void showDialog(String titleText, String buttonLeftText, String buttonRightText) {
        showDialog(titleText, buttonLeftText, buttonRightText, -1);
    }

    public void showDialog(String titleText, String buttonLeftText, String buttonRightText, int rightTextColor) {
        if (mDialog != null) {

            // 弹出对话框
            mDialog.show();

            //加载对话框布局文件
            View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_confirm, null);

            mTitleTv = (TextView) view.findViewById(R.id.tv_title);
            mTitleTv.setText(titleText);
            mLeftBtn = (Button) view.findViewById(R.id.btn_left);
            mLeftBtn.setText(buttonLeftText);
            mRightBtn = (Button) view.findViewById(R.id.btn_right);
            mRightBtn.setText(buttonRightText);
            if (rightTextColor != -1) {
                mRightBtn.setTextColor(rightTextColor);
            }

            mCentreIconIv = (ImageView) view.findViewById(R.id.iv_centre_icon);
            mLeftBtn.setOnClickListener(this);
            mRightBtn.setOnClickListener(this);
            mDialog.setContentView(view);
        }
    }

    public void setMessage(int titleResID) {
        mTitleTv.setText(titleResID);
    }

    public void setMessage(String titleStr) {
        mTitleTv.setText(titleStr);
    }

    /**
     * 关闭对话框
     */
    public void dismissDialog() {
        if (mDialog != null && mDialog.isShowing()) {

            mDialog.dismiss();
        }
    }

    /**
     * 设置对话框是否可以取消（默认可以）
     * @param cancelable
     */
    public void setCancelable(boolean cancelable) {
        mDialog.setCancelable(cancelable);
    }



    public void changeCentreDrawable(int drawableRes){
        mCentreIconIv.setImageResource(drawableRes);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_left) {
            listener.onLeftButtonClick(v);
            dismissDialog();

        } else if (i == R.id.btn_right) {
            listener.onRightButtonClick(v);
            dismissDialog();

        } else {
        }
    }

}
